package kaz;

public abstract class JniObject
{
    static {
        System.loadLibrary("jni");
    }

    public JniObject()
    {
        System.out.println("[JAVA] JniObject: " + this.getClass().getName());
    }

    @Override
    public void finalize() {
        System.out.println("[JAVA] __________________ JniObject::finalize.");
    }

    public void createAndBindNative()
    {
        System.out.println("[JAVA] createAndBindNative: " + this.getClass().getName());
        create_and_bind_object(this.getClass().getName());
    }

    private void bindNative(long o)
    {
        bind_object(o);
    }

    private static void onCreateAndBindJavaObject(String className, long o) {
        System.out.println("[JAVA] __________________ onCreateAndBindJavaObject: " + className);
        try {
            Object jo = Class.forName(className).newInstance();
            if (jo instanceof JniObject) {
                JniObject j = (JniObject)jo;
                j.bindNative(o);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private native void create_and_bind_object(String className);
    private native void bind_object(long o);
    private long native_instance = 0;
}

